# -*- coding:utf-8 -*-
# author:Li Wang
# @Time : 2022/4/14 14:55

#忽略警告
import warnings
warnings.filterwarnings('ignore')

import glob
import pandas as pd
import numpy as np
import os
from datetime import datetime

# Index(['采集时间', '接收时间', '累计行驶里程', '累计运行时间', '累计运行燃料消耗', '发动机内部扭矩', '发动机转速',
#        '循环喷油量', '机油压力', '机油温度', '中冷后进气温度', '增压压力', '发动机冷却液温度', '环境温度', '环境压力',
#        '车速', '油门踏板开度', '离合器状态', '制动开关状态', '空档状态_1', 'vin'],
#       dtype='object')
column_map = {
    # 'vin':'vin',
    '采集时间':'time',
    '循环喷油量':'InjCtl_qCurr',
    '车速':'VehV_v',
    '发动机转速':'Epm_nEng',
    '油门踏板开度':'APP_r',
    '制动开关状态':'Brk_st',
    '离合器状态': 'Clth_st'
}

def excel_to_csv():
    file_to_path = r'F:\wangli\dataset\苏BL6198 4.22-28数据源\苏BL6198 4.22-28数据源'
    filepaths = glob.glob(r'F:\wangli\dataset\苏BL6198 4.22-28数据源\苏BL6198 4.22-28数据源\*.xlsx')
    for each_file in filepaths:
        sheet_dicts = pd.read_excel(each_file,sheet_name=None)
        # overall_df = []
        for key, value in sheet_dicts.items():
            if key == '概述' or key == '详细数据':
                continue
            print('store file')
            value.to_csv(file_to_path+'\\'+each_file.split('\\')[-1].strip('.xlsx')+'_'+key+'.csv',index=False,encoding='utf_8_sig')
        #     overall_df.append(value)
        # print('store file')
        # df.to_csv(file_to_path + '\\'+ each_file.split('\\')[-1].rstrip('.xlsx')+'.csv.gz',index=False,encoding='utf_8_sig')


def data_process(df):
    df.drop(df[(pd.isna(df['VehV_v'])) | (pd.isna(df['InjCtl_qCurr']))].index.to_list(),inplace=True)
    df['time_s'] = df['time'].apply(lambda x: x.split('.')[0])
    df.reset_index(drop=True,inplace=True)
    overall_df = []
    for key,value in df.groupby(by='time_s'):
        overall_df.append(value.head(1))
    df = pd.concat(overall_df)
    df.reset_index(drop=True, inplace=True)
    return df

if __name__ == '__main__':
    # excel_to_csv()
    # exit(0)
    file_source_path = r'F:\wangli\dataset\苏BL6198 4.22-28数据源\苏BL6198 4.22-28数据源\*.csv'
    filepaths = glob.glob(file_source_path)
    filepaths.sort()
    file_working_condition_path = r'F:\wangli\ruixiude_code\daolu_working_condition_identification\datasets\苏BL6198 4.22-28数据源_results\working_condition'
    for each_file_path in filepaths:
        df = pd.read_csv(each_file_path,usecols=column_map.keys())
        df.rename(columns = column_map,inplace=True)
        df = data_process(df)
        df['working_condition'] = np.nan
        #发动机转速为0-》熄火停车
        df['working_condition'].iloc[df[df['Epm_nEng'] == 0].index.to_list()] = '熄火停车'
        #车速大于0-》正常行驶
        df['working_condition'].iloc[df[(df['VehV_v'] > 0) & (df['Epm_nEng'] > 0)].index.to_list()] = '正常行驶'
        #车速为0，油门踏板为0，发动机转速大于590-》装料，卸料，罐体清洗
        df['working_condition'].iloc[df[(df['VehV_v'] == 0) & (df['APP_r'] == 0) & (df['Epm_nEng'] > 590)].index.to_list()] = '装料，卸料，罐体清洗'
        df['working_condition'].iloc[
            df[(df['VehV_v'] == 0) & (df['APP_r'] == 0) & (df['Epm_nEng'] <= 590)].index.to_list()] = '车辆驻车，发动机怠速'
        print('store file')
        df.to_csv(file_working_condition_path+'\\'+each_file_path.split('\\')[-1].rstrip('.csv')+'_working_condition.csv',index=False,encoding='utf_8_sig')

